package com.example.yiwei.crm.view.activity.healthActivity;

import android.content.Intent;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.dockDao;
import com.example.yiwei.crm.dao_impl.dockDaoImpl;
import com.example.yiwei.crm.data.GetActivity;
import com.example.yiwei.crm.data.GetDock;
import com.example.yiwei.crm.data.GetPersonnal;
import com.example.yiwei.crm.data.GetVisit;
import com.example.yiwei.crm.entity.activityEntity;
import com.example.yiwei.crm.entity.dockingEntity;
import com.example.yiwei.crm.entity.personnalEntity;
import com.example.yiwei.crm.util.NetWorkUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

public class addActivity extends AppCompatActivity {
private EditText edit_theme,edit_task_description;
    private ImageView img_back,img_commit;
    private Spinner spinner_department,spinner_task_state,spinner_priority;
    private TextView txt_manager_name,txt_participants,txt_activity_time,txt_department,txt_title;
    private Button btn_show,btn_select,btn_select1,btn_select2;
    List<Map<String,String>> YXJBSpinnerList;
    List<Map<String,String>> ZTSpinnerList;
    List<Map<String,String>> BMSpinnerList;
    List<String> YXJBList;
    List<String> ZTList;
    List<String> BMList;
    private ArrayAdapter adapter;
    private String priority,task_state,department;
    private int priority_id,task_state_id,department_id;
    private TimePickerView mpv;
    private String visit_time;
    List<String> list;
    dockingEntity dockingEntity;
    dockingEntity dockingEntity1;
    dockingEntity dockingEntity2;
    dockingEntity dockingEntity3;
    activityEntity activityEntity;
    personnalEntity personnalEntity;
    String result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        edit_theme= (EditText) findViewById(R.id.add_activity_edit_theme);
        txt_title= (TextView) findViewById(R.id.add_activity_txt_title);
        edit_task_description= (EditText) findViewById(R.id.add_activity_edit_task_description);
        spinner_priority= (Spinner) findViewById(R.id.add_activity_spinner_priority);
        spinner_task_state= (Spinner) findViewById(R.id.add_activity_spinner_task_state);
        txt_activity_time= (TextView) findViewById(R.id.add_activity_txt_activity_time);
        txt_manager_name= (TextView) findViewById(R.id.add_activity_txt_manager_name);
        txt_participants= (TextView) findViewById(R.id.add_activity_participants);
        txt_department= (TextView) findViewById(R.id.add_activity_txt_department);
        img_back= (ImageView) findViewById(R.id.add_activity_img_back);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        img_commit= (ImageView) findViewById(R.id.add_activity_img_commit);
        btn_select= (Button) findViewById(R.id.add_activity_btn_select);
        btn_select1= (Button) findViewById(R.id.add_activity_btn_select1);
        btn_select2= (Button) findViewById(R.id.add_activity_btn_select2);
        btn_show= (Button) findViewById(R.id.add_activity_btn_show);
       GetDock.create1();
        GetDock.create2();
       GetDock.create3();
        if ("update".equals(getIntent().getStringExtra("tag"))){
            txt_title.setText("修改活动信息");
        }
        //创建选择器
        mpv = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar = Calendar.getInstance();
        mpv.setRange(calendar.get(Calendar.YEAR) - 100, calendar.get(Calendar.YEAR) + 100);
        //插入时间
        mpv.setTime(new Date());

        //选择监听
        mpv.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                visit_time=getTime(date);
                txt_activity_time.setText(visit_time);
            }
        });
        btn_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mpv.show();
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                result= NetWorkUtils.getCount(baseClass.find_visit_spinner_url,null);
                dockDao dockDao=new dockDaoImpl();
                YXJBSpinnerList=dockDao.queryDockingSpinnerMessage(result,1);
                ZTSpinnerList=dockDao.queryDockingSpinnerMessage(result,2);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        YXJBList=new ArrayList<String>();
                        ZTList=new ArrayList<String>();
                        BMList=new ArrayList<String>();
                        YXJBList.add(0,"");
                        for (int i=0;i<YXJBSpinnerList.size();i++){
                            YXJBList.add(YXJBSpinnerList.get(i).get("YXJB02"));
                        }
                        adapter = new ArrayAdapter(addActivity.this,android.R.layout.simple_spinner_item, YXJBList);
                        spinner_priority.setAdapter(adapter);
                        spinner_priority.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                                priority=spinner_priority.getSelectedItem().toString();
                                for (int i1=0;i1<YXJBSpinnerList.size();i1++){
                                    if (priority.equals(YXJBSpinnerList.get(i1).get("YXJB02"))){
                                        priority_id=Integer.valueOf(YXJBSpinnerList.get(i1).get("YXJB01"));
                                    }
                                }
                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> adapterView) {

                            }
                        });
                        ZTList.add(0,"");
                        for (int i=0;i<ZTSpinnerList.size();i++){
                            ZTList.add(ZTSpinnerList.get(i).get("ZT02"));
                        }
                        adapter = new ArrayAdapter(addActivity.this,android.R.layout.simple_spinner_item, ZTList);
                        spinner_task_state.setAdapter(adapter);
                        spinner_task_state.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                                task_state=spinner_task_state.getSelectedItem().toString();
                                for (int i1=0;i1<ZTSpinnerList.size();i1++){
                                    if (task_state.equals(ZTSpinnerList.get(i1).get("ZT02"))){
                                        task_state_id=Integer.valueOf(ZTSpinnerList.get(i1).get("ZT01"));
                                    }
                                }

                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> adapterView) {

                            }
                        });
                        if ("update".equals(getIntent().getStringExtra("tag"))) {
                            activityEntity = GetActivity.get();
                            SpinnerAdapter spadapter2 = spinner_task_state.getAdapter();
                            Integer i2 =activityEntity.getTask_state_id();
                            if (i2!=null){
                                for (int i = 0;i<ZTSpinnerList.size();i++){
                                    if (i2==Integer.valueOf(ZTSpinnerList.get(i).get("ZT01"))){
                                        task_state= ZTSpinnerList.get(i).get("ZT02");
                                    }
                                }
                            }

                            if (task_state!=null){
                                for (int i = 0;i<spadapter2.getCount();i++){
                                    if (task_state.equals(spadapter2.getItem(i).toString())){
                                        spinner_task_state.setSelection(i);
                                    }
                                }
                            }

                            SpinnerAdapter spadapter1 = spinner_priority.getAdapter();
                            Integer i1 =activityEntity.getPriority_id();
                            if (i1!=null){
                                for (int i = 0;i<YXJBSpinnerList.size();i++){
                                    if (i1==Integer.valueOf(YXJBSpinnerList.get(i).get("YXJB01"))){
                                        priority= YXJBSpinnerList.get(i).get("YXJB02");
                                    }
                                }
                            }

                            if (priority!=null){
                                for (int i = 0;i<spadapter1.getCount();i++){
                                    if (priority.equals(spadapter1.getItem(i).toString())){
                                        spinner_priority.setSelection(i);
                                    }
                                }
                            }


                        }

                    }
                });
            }
        }).start();
        if ("update".equals(getIntent().getStringExtra("tag"))){
               activityEntity= GetActivity.get();
               edit_theme.setText(activityEntity.getActivity_theme());
               txt_department.setText(activityEntity.getActivity_department());
               txt_manager_name.setText(activityEntity.getActivity_manager());
               txt_participants.setText(activityEntity.getActivity_participation());
            if (activityEntity.getActivity_time() != null) {
                SimpleDateFormat sdf = new SimpleDateFormat(" yyyy-MM-dd  ");
                sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
                String activity_time = activityEntity.getActivity_time();
                long l = Long.parseLong(activity_time);
                Date date = new Date(l);
                String cTime = sdf.format(date);
                txt_activity_time.setText(cTime);
            }
               edit_task_description.setText(activityEntity.getActivity_description());

        }
        btn_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              Intent intent=new Intent(addActivity.this,departmentSelectActivity.class);
                startActivity(intent);
            }
        });
        btn_select1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              Intent intent=new Intent(addActivity.this,participationActivity.class);
                intent.putExtra("tag","manager");
                startActivity(intent);
            }
        });
        btn_select2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(addActivity.this,participationActivity.class);
                intent.putExtra("tag","participation");
                startActivity(intent);
            }
        });
        img_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        JSONObject object=new JSONObject();
                        try {
                            object.put("KRA02",edit_theme.getText().toString());
                            object.put("KRA15",GetDock.dockingEntity1.getManager_department_id());
                            object.put("KRA03",GetDock.dockingEntity3.getManager_id());
                            object.put("KRA04",GetDock.dockingEntity2.getParticipation_id());
                            object.put("KRA16",GetDock.dockingEntity1.getManager_department());
                            Log.d("oo",GetDock.dockingEntity1.getManager_department());
                            Log.d("ff",GetDock.dockingEntity3.getManager());
                            object.put("KRA17",GetDock.dockingEntity3.getManager());
                            object.put("KRA18",GetDock.dockingEntity2.getParticipation_name());
                            object.put("AKJ01",priority_id);
                            object.put("AKK01",task_state_id);
                            object.put("KRA07",txt_activity_time.getText().toString());
                            object.put("KRA09",edit_task_description.getText().toString());
                            object.put("KDA01",getIntent().getExtras().getInt("docking_id"));
                            personnalEntity= GetPersonnal.get();
                            object.put("CID01",personnalEntity.getCurrent_create_id());
                            object.put("CID03",personnalEntity.getPersonnal_name());
                            if ("update".equals(getIntent().getStringExtra("tag"))){
                                object.put("UID01",-1);
                            }else{
                                object.put("CID01",-1);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String json=object.toString();
                        if ("update".equals(getIntent().getStringExtra("tag"))){
                             result=NetWorkUtils.getCount(baseClass.edit_activity_message_url+getIntent().getExtras().getInt("activity_id"),json);

                        }else{
                            result=NetWorkUtils.getCount(baseClass.add_activity_url,json);
                        }
                        JSONObject object1= null;
                        try {
                            object1 = new JSONObject(result);
                            String boolen=object1.getString("boolean");
                            Log.d("lll",boolen);
                            if ("success".equals(boolen)){
                                if ("update".equals(getIntent().getStringExtra("tag"))){
                                    GetActivity.create();
                                    activityEntity.setActivity_theme(edit_theme.getText().toString());
                                    activityEntity.setActivity_department(txt_department.getText().toString());
                                    activityEntity.setActivity_manager(txt_manager_name.getText().toString());
                                    activityEntity.setActivity_participation(txt_participants.getText().toString());
                                    activityEntity.setActivity_time(txt_activity_time.getText().toString());
                                    activityEntity.setPriority(priority);
                                    activityEntity.setTask_state(task_state);
                                    activityEntity.setActivity_description(edit_task_description.getText().toString());
                                    GetActivity.activityEntity=activityEntity;
                                    if (GetActivity.activityEntity!=null){
                                        finish();
                                    }
                                }
                                finish();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }).start();
            }
        });
    }
    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);

    }
    @Override
    protected void onRestart() {
        super.onRestart();
        dockingEntity1=GetDock.get1();
        dockingEntity2=GetDock.get2();
        dockingEntity3=GetDock.get3();
        if (dockingEntity1!=null){
            txt_department.setText(dockingEntity1.getManager_department());
        }
        if (dockingEntity2!=null){
            txt_participants.setText(dockingEntity2.getParticipation_name());
        }
        if (dockingEntity3!=null){
            txt_manager_name.setText(dockingEntity3.getManager());
        }
    }
}
